﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YJKArchMethodLibrary.Utils;

namespace YJKAxisAndEdit
{
	internal class HideOrShowKit
	{
		public static void HideCharact(int nHasTag, Document Doc, ElementId GridId)
		{
			Element element = Doc.GetElement(GridId);
			Grid grid = element as Grid;
			if (!(element is Grid))
			{
				return;
			}
			grid = (element as Grid);
			if (grid.IsHidden(Doc.ActiveView))
			{
				return;
			}
			GridType gridType = Doc.GetElement(grid.GetTypeId()) as GridType;
			if (!gridType.Name.Contains("_NoTag"))
			{
				GridType gridNewType = HideOrShowKit.GetGridNewType(Doc, gridType);
				grid.ChangeTypeId(gridNewType.Id);
			}
			if (nHasTag == 0 || nHasTag == 2)
			{
				grid.ShowBubbleInView((DatumEnds)0, Doc.ActiveView);
			}
			if (nHasTag == 1 || nHasTag == 2)
			{
                grid.ShowBubbleInView((DatumEnds)1, Doc.ActiveView);
			}
		}

		public static void HideCharactAndCircle(int nHasTag, Document Doc, ElementId GridId)
		{
			Grid grid = Doc.GetElement(GridId) as Grid;
			if (grid.IsHidden(Doc.ActiveView))
			{
				return;
			}
			GridType gridType = Doc.GetElement(grid.GetTypeId()) as GridType;
			if (gridType.Name.Contains("_NoTag"))
			{
				GridType gridNewType = HideOrShowKit.GetGridNewType(Doc, gridType);
				grid.ChangeTypeId(gridNewType.Id);
			}
			if (nHasTag == 0 || nHasTag == 2)
			{
				grid.HideBubbleInView((DatumEnds)0, Doc.ActiveView);
			}
			if (nHasTag == 1 || nHasTag == 2)
			{
				grid.HideBubbleInView((DatumEnds)1, Doc.ActiveView);
			}
		}

		public static void ShowBubbles(int nHasTag, Document Doc, ElementId GridId)
		{
			Grid grid = Doc.GetElement(GridId) as Grid;
			if (grid.IsHidden(Doc.ActiveView))
			{
				return;
			}
			GridType gridType = Doc.GetElement(grid.GetTypeId()) as GridType;
			if (gridType.Name.Contains("_NoTag"))
			{
				GridType gridNewType = HideOrShowKit.GetGridNewType(Doc, gridType);
				grid.ChangeTypeId(gridNewType.Id);
				return;
			}
			if (nHasTag == 0 || nHasTag == 2)
			{
				grid.ShowBubbleInView((DatumEnds)0, Doc.ActiveView);
			}
			if (nHasTag == 1 || nHasTag == 2)
			{
				grid.ShowBubbleInView((DatumEnds)1, Doc.ActiveView);
			}
		}

		private static GridType GetGridNewType(Document Doc, GridType gtOldType)
		{
			GridType gridType = null;
			if (gtOldType.Name.Contains("_NoTag"))
			{
				string text = gtOldType.Name;
				text = text.Substring(0, text.IndexOf("_NoTag"));
				List<GridType> gridTypes = Common.GetGridTypes(Doc);
				using (List<GridType>.Enumerator enumerator = gridTypes.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						Element element = enumerator.Current;
						GridType gridType2 = element as GridType;
						if (gridType2.Name.CompareTo(text) == 0)
						{
							gridType = gridType2;
							break;
						}
					}
					return gridType;
				}
			}
			string text2 = gtOldType.Name + "_NoTag";
			List<GridType> gridTypes2 = Common.GetGridTypes(Doc);
			foreach (Element element2 in gridTypes2)
			{
				GridType gridType3 = element2 as GridType;
				if (gridType3.Name.CompareTo(text2) == 0)
				{
					gridType = gridType3;
					break;
				}
			}
			if (gridType == null)
			{
				SubTransaction subTransaction = new SubTransaction(Doc);
				try
				{
					subTransaction.Start();
					gridType = (gtOldType.Duplicate(text2) as GridType);
					FamilySymbol gridTag = HideOrShowKit.GetGridTag("Grid_Head_Null", Doc);
					if (gridTag != null)
					{
						gridType.get_Parameter(BuiltInParameter.GRID_HEAD_TAG).Set(gridTag.Id);
					}
					subTransaction.Commit();
				}
				catch (Exception ex)
				{
					YJKMessageBox.Show(ex.Message);
					subTransaction.RollBack();
					return null;
				}
				return gridType;
			}
			return gridType;
		}

		private static FamilySymbol GetGridTag(string tagName, Document Doc)
		{
			FamilySymbol result = null;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(Doc);
			filteredElementCollector.OfClass(typeof(FamilySymbol)).ToElements();
			foreach (Element element in filteredElementCollector)
			{
				FamilySymbol familySymbol = (FamilySymbol)element;
				if (familySymbol.Category.Id.IntegerValue == -2006040 && familySymbol.Name.CompareTo(tagName) == 0)
				{
					result = familySymbol;
					break;
				}
			}
			return result;
		}
	}
}
